﻿/*
File Name:      uknow_search.js
Created By:     victor.liu
Created When:   2012-04-06
Created Whys:   This js file is created mainly for search.html
*/
var g_currentPageIndexes = new Array(3);
g_currentPageIndexes[0] = 0;
g_currentPageIndexes[1] = 0;
g_currentPageIndexes[2] = 0;

var g_pageSizes = new Array(3);
g_pageSizes[0] = 10;
g_pageSizes[1] = 15;
g_pageSizes[2] = 15;

var g_searchEngines = new Array(3);
g_searchEngines[0] = null;
g_searchEngines[1] = null;
g_searchEngines[2] = null;

Request = {
	QueryString : function(item){
		var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
		return svalue ? svalue[1] : svalue;
	}
};

function searchReq(type, encKey){
	this.type = type;
	this.encodedKey = encKey;
	
	this.buildRequestUrl = function (){
		var type = this.type;
		var encKey = this.encodedKey;
		var pageSize = g_pageSizes[type];
		var pageIndex = g_currentPageIndexes[type];
		var url = "/GateService/SearchGate.aspx?lang=" + g_langID + "&key=" + encKey + "&type=" + type + "&pageSize=" + pageSize + "&pageIndex=" + pageIndex + "&ram=" + new Date().toLocaleString();
		return url;
	};
	
	this.query = function(){
		var url = this.buildRequestUrl();
		var req = new ajaxRequest(url);
		req.async = false;
		var result = null;
		req.get(function(res){
			result = res;
		});
		
		if (this.type == 0)
			this.renderDocs(result);
		else if (this.type == 1)
			this.renderImages(result);
		else if (this.type == 2)
			this.renderVideos(result);
	};
	
	this.renderDocs = function(docs){
		var ulDocs = $('.resultxt');
		ulDocs.html('');
		
		if (docs == null || typeof docs == 'undefined') return;
		$('.searchresult p span:eq(0)').html(docs.Capacity);
		var length = docs.List.length;
		
		for (var i = 0; i < length; i++){
			var doc = docs.List[i];
			var li = $('<li></li>');
			var abbr = $('<abbr></abbr>');
			var a = $('<a></a>').attr('href', doc.Url).html(doc.Title);
			abbr.append(a);
			
			var contentAnchor = $('<a></a>').attr('href', doc.Url).html(doc.Content);
			li.append(abbr).append(contentAnchor);
			ulDocs.append(li);
		}
		
		renderPager(this, docs.Capacity);
	};
	
	this.renderImages = function(images){
		var ulImages = $('.resultimg:eq(0)');
		ulImages.html('');
		
		if (images == null || typeof images == 'undefined') return;
		$('.searchresult p span:eq(1)').html(images.Capacity);
		
		var length = images.List.length;
		for (var i = 0; i < length; i++){
			var image = images.List[i];
			//<li><a href="#"><img src="images/result_1.jpg" alt=""></a><abbr><a href="#">Tony Cragg sculptures and drawings</a></abbr></li>
			var li = $('<li></li>');
			var img = $('<img></img>').attr('src', Environment.FilePath.UploadImagePath + '/' + image.Path);
			var url = '/' + g_siteroot + Environment.FilePath.EventsPath + '/' + image.HtmlFileName;
			var firstAnchor = $('<a></a>').attr('href', url).append(img);
			
			var abbr = $('<abbr></abbr>');
			var a = $('<a></a>').attr('href', url).html(image.Name);
			abbr.append(a);
			
			li.append(firstAnchor).append(abbr);
			ulImages.append(li);
		}
		
		renderPager(this, images.Capacity);
	};
	
	this.renderVideos = function(videos){
		var ulVideos = $('.resultimg:eq(1)');
		ulVideos.html('');
		
		if (videos == null || typeof videos == 'undefined') return;
		$('.searchresult p span:eq(2)').html(videos.Capacity);
		
		var length = videos.List.length;
		for (var i = 0; i < length; i++){
			var video = videos.List[i];
			//<li><a href="#"><img src="images/result_1.jpg" alt=""></a><abbr><a href="#">Tony Cragg sculptures and drawings</a></abbr></li>
			var li = $('<li></li>');
			var img = $('<img></img>').attr('src', Environment.FilePath.UploadImagePath + '/' + video.Path);
			var url = '/' + g_siteroot + Environment.FilePath.EventsPath + '/' + video.HtmlFileName;
			var firstAnchor = $('<a></a>').attr('href', url).append(img);
			
			var abbr = $('<abbr></abbr>');
			var a = $('<a></a>').attr('href', url).html(video.Name);
			abbr.append(a);
			
			li.append(firstAnchor).append(abbr);
			ulVideos.append(li);
		}
		
		renderPager(this, videos.Capacity);
	};
};

function renderPager (search, totalCount) {
	var num = search.type;
	var pageSize = g_pageSizes[num];
	var currentPageIndex = g_currentPageIndexes[num];
	
	$('.subtitle:eq('+ num + ') ul').html('');
	
    var pageCount = Math.floor(totalCount / pageSize);
    if (totalCount % pageSize > 0) pageCount++;

    if (pageCount <= 1) return;
	//上一页
	var aPrev = $('<a></a>').html(searchConstants.prev).attr('href', 'javascript:jumpPage("p", ' + pageCount + ',' + num + ');');
	var liPrev = $('<li></li>').append(aPrev);

    //页码
    var pageNum = (totalCount == 0) ? 0 : currentPageIndex + 1;
	var span = $('<span></span>').html(pageNum + '/' + pageCount);
	var liPage = $('<li></li>').append(span);

    //下一页
    var aNext = $('<a></a>').html(searchConstants.next).attr('href', 'javascript:jumpPage("n", ' + pageCount + ',' + num + ');');
    var liNext = $('<li></li>').append(aNext);

	var spanGoto = $('<span></span>').html(searchConstants.go);
	var liGoto = $('<li></li>').append(spanGoto);
	
	var txtGo = $('<abbr><input id="txtPage_' + num + '" type="text"/></abbr>');
	var liTxtGo = $('<li></li>').append(txtGo);
	
	var btnGo = $("<input name='' type='button' value='' onclick='javascript:goPage(" + pageCount + "," + num + ");'/>");
    var liBtn = $('<li></li>').append(btnGo);
	
	$('.subtitle:eq(' + num + ') ul').append(liPrev).append(liPage).append(liNext).append(liGoto).append(liTxtGo).append(liBtn);
};

function validateInt(str, msg) {
    if (/^[1-9]+[0-9]*]*$/.test(str)) {
        return true;
    } else {
        alert(msg);
        return false;
    }
};

function retrieveAlterMessage(pageCount)
{
	if (g_langID == 2052)
		return '请输入 1 - ' + pageCount + ' 之间的整数！';
	else
		return 'Please input page number between 1 and ' + pageCount;
};

function goPage (pageCount, num) {
	var page = $('#txtPage_' + num).val();
	var msg = retrieveAlterMessage(pageCount);
    if (validateInt(page, msg)) {
		if (parseInt(page)>pageCount){
			alert(msg);
			return;
		}
        
		g_currentPageIndexes[num] = parseInt(page) - 1;
        g_searchEngines[num].query();
    }
};

function jumpPage(dir, pageCount, type){
	switch (dir) {
		case "f":
			g_currentPageIndexes[type] = 0;
			break;
		case "p":
			g_currentPageIndexes[type]--;
			break;
		case "n":
			g_currentPageIndexes[type]++;
			break;
		case "l":
			g_currentPageIndexes[type] = pageCount - 1;
			break;
	}
	
	if (g_currentPageIndexes[type] < 0) 
		g_currentPageIndexes[type] = 0;
	if (g_currentPageIndexes[type] >= pageCount) 
		g_currentPageIndexes[type] = pageCount - 1;
	
	g_searchEngines[type].query();
};

function clearSearchResult(){
	$('.resultxt').html('');
	$('.resultimg:eq(0)').html('');
	$('.resultimg:eq(1)').html('');
	
	$('.subtitle:eq(0) ul').html('');
	$('.subtitle:eq(1) ul').html('');
	$('.subtitle:eq(2) ul').html('');
	
	$('.searchresult p span:eq(0)').html('0');
	$('.searchresult p span:eq(1)').html('0');
	$('.searchresult p span:eq(2)').html('0');
};

function initSearchPage(){
	clearSearchResult();
	var encKey = Request.QueryString('key');
	if (encKey == null || typeof encKey == 'undefined' || encKey == String.Empty){
		return;
	}

    //$('.resultform input:eq(0)').val(encKey.fromBase64String());
    $('.resultform input:eq(0)').val(decodeURIComponent(encKey));
	var textSearch = new searchReq(0, encKey);
	textSearch.query();
	var imgSearch = new searchReq(1, encKey);
	imgSearch.query();
	var videoSearch = new searchReq(2, encKey);
	videoSearch.query();
	
	g_searchEngines[0] = textSearch;
	g_searchEngines[1] = imgSearch;
	g_searchEngines[2] = videoSearch;
};

$(function(){
	var btnSearch = $('.resultform input:eq(1)');
	var ctrl = $('.resultform input:eq(0)');
    btnSearch.click(function () { new search(g_langID, ctrl).exec(); });
	
	initSearchPage();
});
